{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "\n",
    "Python\n",
    "------\n",
    "\n",
    "In case you are wondering, we will be using Python 3.3. \n",
    "\n",
    "iPython Notebook\n",
    "----------------\n",
    "\n",
    "We will be working with Python in your browser using iPython Notebook. Here is an example block of code:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print('Hello, World!')\n",
    "# This is a comment, it isn't run as code, but often they are helpful"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To run a block of code like the one above, click on it to select it and then you can either click the run button in the menu above:\n",
    "\n",
    "<button id=\"run_b\" title=\"Run Cell\" class=\"ui-button ui-widget ui-state-default ui-button-icon-only ui-corner-left\" role=\"button\" aria-disabled=\"false\"><span class=\"ui-button-icon-primary ui-icon ui-icon-play\"></span><span class=\"ui-button-text\">Run Cell</span></button>\n",
    "\n",
    "or type shift-enter. The output of the block is shown below the block.\n",
    "\n",
    "All of the code blocks on this page are interactive. Please make sure you run them all at least once. Feel free the change the code and see what the affect is."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Part 1\n",
    "======\n",
    " \n",
    "Arithmetic\n",
    "----------\n",
    "\n",
    "Like every programming language, Python is a good calculator. Run the block of code below to make sure the answer is right!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "1 + 1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Hopefully it worked. \n",
    "\n",
    "Now lets say you won the lottery. You are about to collect your millions, but first you have to answer this skill testing question:\n",
    "\n",
    "\"8+6*2*3-(15-13)\"\n",
    "\n",
    "Fortuntely Python can help. The order of operations you learned in school applies, BEDMAS (brackets, exponents, division, multiplication, addition, subtraction)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "8 + 6*2*3 - (15 - 13)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Numbers are valid Python code as are the common operators, +, /, * and -. You can write different types of numbers including integers, real numbers (floating point) and negative integers."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "42 + 3.149 + -1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since 42 is literally 42, we call these numbers *literals*. You are literally writing number in your Python code.\n",
    "\n",
    "Check Please\n",
    "------------\n",
    "\n",
    "So you just had a big meal to celebrate your winnings, and now you need to calculate the tip."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "meal = 200.00\n",
    "# as a decimal, 10% would be 0.1\n",
    "tip_percent = 0.10\n",
    "meal * tip_percent"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to make it more user friendly you could do the following."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "meal = 200.00\n",
    "# as integer, 10% would be 10\n",
    "tip_percent = 10\n",
    "meal * tip_percent / 100"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because of BEDMAS we don't need brackets, but meal * (tip_percent / 100) would work too."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Variables\n",
    "----------\n",
    "\n",
    "meal and tip_percent aren't literal numbers, they are variables.\n",
    "\n",
    "In Python variables are like buckets (dump trucks?). You can put anything you want in them. Just give them a name and you can use them in place of the literal value.\n",
    "\n",
    "Above meal was 200.00 but we could also set meal to the text 'Hello, World'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "meal = 200.00\n",
    "print(meal)\n",
    "meal = \"Hello, World!\"\n",
    "print(meal)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The value a variable has only depends on what it was last assigned.\n",
    "\n",
    "It is like a spreadsheet except you choose the names for the cells yourself.\n",
    "\n",
    "Exceptional Python\n",
    "-------------------\n",
    "\n",
    "Python only understands certain code. When you write something Python doesn't understand it throws an exception and tries to explain what went wrong, but it can only speak in a broken Pythonesque english. Let's see some examples by running these code blocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "gibberish"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "*adsflf_"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print('Hello'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "1v34"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "2000 / 0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Python tries to tell you where it stopped understanding, but in the above examples, each program is only 1 line long. \n",
    "\n",
    "It also tries to show you where on the line the problem happened with caret (\"^\"). \n",
    "\n",
    "Finally it tells you the type of thing that went wrong, (NameError, SyntaxError, ZeroDivisionError) and a bit more information like \"name 'gibberish' is not defined\" or \"unexpected EOF while parsing\".\n",
    "\n",
    "Unfortunately you might not find \"unexpected EOF while parsing\" too helpful. EOF stands for End of File, but what file? What is parsing? Python does it's best, but it does take a bit of time to develop a knack for what these messages mean. \n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Part 2\n",
    "======\n",
    "\n",
    "The Written Word\n",
    "----------------\n",
    "\n",
    "Numbers are great... but most of our day to day computing needs involves text, from emails to tweets to documents.\n",
    "\n",
    "We have already seen a text literal in Python, \"Hello, World!\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"Hello, World!\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Text literals are surrounded by quotes. Without the quotes Hello by itself would be viewed as a variable name.\n",
    "\n",
    "You can use either double quotes (\") or single quotes (') for text literals.\n",
    "\n",
    "As we saw before we can also save text literals in variables.\n",
    "\n",
    "What's a String?\n",
    "----------------\n",
    "\n",
    "Programmers call text literals *strings* because we are weird like that. From now on we will only refer to strings, but we just mean pieces of text inside our code.\n",
    "\n",
    "Let's use strings with variables!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "your_name = \"Albert O'Connor\"\n",
    "print(\"Hello, \")\n",
    "print(your_name)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Strings in Python are a bit more complicated because the operations on them aren't just + and * (though those are valid operations).\n",
    "\n",
    "Strings have their own operations we can call on them to change them. We can use dir to get an idea of what they are."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dir(\"Hello, World!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That is a really long list. For now you can ignore the ones which start and end with underscores (\"_\"), but that still leaves a lot! Let's start with upper and lower.\n",
    "\n",
    "Let's say you are really happy to the world, and you want to make sure everyone knows it, you can use upper. \"upper\" is short for uppercase and you will see what it does by running the code block below."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "string = \"Hello, World\"\n",
    "string.upper()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Maybe you are feeling a bit sad and you want to be quiet, you can then use lower."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "string = \"Hello, World\"\n",
    "string.lower()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We use the dot (\".\") operator to call these operations on the string. What lower and upper operate on comes before the dot and needs to be a string variable or literal."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"Hello, World\".upper()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Formating\n",
    "---------\n",
    "\n",
    "Sometimes you want to create a string out of a few other strings. Above we printed\n",
    "\n",
    "    Hello,\n",
    "    Your Name\n",
    "\n",
    "by using two print statements, but it would be nice to output \"Hello, Your Name!\" instead. (Where Your Name is actually your name... oh variables)\n",
    "\n",
    "We can do this with the string operation called format. Format is different from lower and upper because it can take other arguments. An *agrument* is what coders call the value passed to an operation or function, it doesn't mean we are fighting."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "your_name = \"Albert O'Connor\"\n",
    "string = \"Hello, {0}!\"\n",
    "print(string.format(your_name))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also used literal strings:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(\"Hello, {0}!\".format(\"Albert O'Connor\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "{0} is kind of weird. {0} is  where the first argument, \"Albert O'Connor\", to format() is placed in the resulting text. {1} would mean use the second value if there was one. See below!\n",
    "\n",
    "Indexed by Zero\n",
    "---------------\n",
    "\n",
    "For better or worse, (and practically it is better most of the time) everything in Python is index by 0. We will see this over and over again but for now if you call format like this:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"{0} likes {1}\".format(\"Albert O'Connor\", 'Python')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We would call \"Albert O'Connor\" the 0th string passed into format and 'Python' the 1st. It is kind of weird, but roll with it. It will eventually make things eaiser.\n",
    "\n",
    "Line Endings\n",
    "------------\n",
    "\n",
    "Let's say we wanted to represent the string in one string variable?\n",
    "\n",
    "    200 University Ave.\n",
    "    Waterloo, ON\n",
    "\n",
    "A line ending is one example of something which is part of text on the screen that we need to somehow represent in a string. The key is the backslash (\"\\\") character. \"\\n\" and \"\\r\\n\" represents two kind of line endings. Try adding \"\\n\" in the right place to make it appear like the text at the beginning of this section."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(\"200 University Ave. Waterloo, ON\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exercise\n",
    "---------\n",
    "\n",
    "Alice is sending a short email message to Bob. She wants to format the message on screen so she knows what she is sending. She is using the string called template below. Change the value of the template string to make it better. You can run the block to get further instructions."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Edit this string\n",
    "template =\"{0} {1} {2} {3}\"\n",
    "\n",
    "# Leave this alone please, it will help you as you go through the exercise\n",
    "check('p1', template.format(\"alice@domain.org\", \"bob@domain.org\", \"Alice's Subject\", \"This is my one line message!\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Part 3\n",
    "======\n",
    "\n",
    "If Else\n",
    "-------\n",
    "\n",
    "Has an application ever ask you a question? Maybe it asks you if you really want to quit because unsaved changed might lost, or if you want to leave a webpage. If you answer OK one thing happen, like your application closing, but if you answer No or Cancel something else happens. In all those cases there is a special piece of code that is being run somewhere, it is an *if* condition.\n",
    "\n",
    "Like all languages, Python allows us to conditionally run code.\n",
    "\n",
    "To have an if condition we need the idea of something being true and something being false. Remember, we call numbers \"integers\" and \"floating point\", and text \"strings\". We call true or false \"boolean\" values. True would represent OK where as false would represent No or Cancel in the example above.\n",
    "\n",
    "The literal values in Python for true and false are \"True\" and \"False\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "False is False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "True is True"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "True is False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "true is False"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can write expressions with operations too."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "1 > 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"Cool\".startswith(\"C\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"Cool\".endswith(\"C\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "\"oo\" in \"Cool\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "42 == 1 # note the double equals sign for equality"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In order to write an \"if\" statement we need code that spans multiple lines\n",
    "\n",
    "    if condition:\n",
    "        print(\"Condition is True\")\n",
    "    else:\n",
    "        print(\"Condition is False\")\n",
    "\n",
    "Some things to notice. The if condition ends in a colon (\":\"). In Python blocks of code are indicated with a colon (\":\") and are grouped by white space. Notice the else also ends with a colon (\":\"), \"else:\". Let's try changing the condition and see what happens."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "condition = 1 > 2\n",
    "if condition:\n",
    "    print(\"Condition is True\")\n",
    "else:\n",
    "    print(\"Condition is False\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "About that white space, consider the following code:\n",
    "\n",
    "    if condition:\n",
    "        print(\"Condition is True\")\n",
    "    else:\n",
    "        print(\"Condition is False\")\n",
    "    print(\"Condition is True or False, either way this is outputted\")\n",
    "\n",
    "Since the last print statement isn't indented it gets run after the if block or the else block.\n",
    "\n",
    "You can play with this. Try indenting the last print statement below and see what happens."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "condition = True\n",
    "if condition:\n",
    "    print(\"Condition is True\")\n",
    "else:\n",
    "    print(\"Condition is False\")\n",
    "print(\"Condition is True or False, either way this is outputted\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exercise\n",
    "---------\n",
    "\n",
    "You can also use \"and\" and \"or\" to combine conditions. Let's look at and.\n",
    "\n",
    "    True and True is True\n",
    "    True and False is False\n",
    "    False and True is False\n",
    "    False and False is False\n",
    "\n",
    "With \"and\" both conditions have to be True to be True. \n",
    "\n",
    "Below change the values of the three variables to make the entire \"if condition\" true."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Edit the values of these 3 variables\n",
    "boolean_literal = False\n",
    "number = 8\n",
    "string_literal = \"I like to count sheep before bed.\"\n",
    "\n",
    "# Leave this code the same please\n",
    "if number > 10 and boolean_literal and \"cows\" in string_literal:\n",
    "    print(\"Success!\")\n",
    "else:\n",
    "    print(\"Try again!\")\n",
    "\n",
    "# This just provides some hints\n",
    "check(\"p3\", (number, boolean_literal, string_literal)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Part 4\n",
    "======\n",
    "\n",
    "Lists\n",
    "-----\n",
    "\n",
    "So far we have numbers, strings, and conditional if statements. Now for our first container &mdash; a list.\n",
    "\n",
    "A list in Python is just like a shopping list or a list of numbers. They have a defined order and you can add to it or remove from it.\n",
    "\n",
    "Let's take a look at some simple lists."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# The empty list\n",
    "[]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "[\"Milk\", \"Eggs\", \"Bacon\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "[1,2,3]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "List literals are all about square brackets (\"[ ]\") and commas (\",\"). You can create a list of literals by wrapping them in square brackets and separating them with commas.\n",
    "\n",
    "You can even mix different types of things into the same list; numbers, strings, booleans."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "[True, 0, \"Awesome\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can put variables into a list and set a variable to a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "your_name = \"Albert O'Connor\"\n",
    "awesome_people = [\"Eric Idle\", your_name]\n",
    "print(awesome_people)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Like strings lists have operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "dir([])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\"append\" is an interesting one. \"append\" lets you add an item to the end of a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "your_name = \"Albert O'Connor\"\n",
    "awesome_people = [\"Eric Idle\", your_name]\n",
    "awesome_people.append(\"John Cleese\")\n",
    "print(awesome_people)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can use square brackets (\"[]\") again with the variable of the list to access individual elements."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "awesome_people[0]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is that 0 indexing again. The first element of the list is given index value 0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print(\"These people are awesome: {0}, {1}, {2}\".format(awesome_people[0], awesome_people[1], awesome_people[2]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Loops\n",
    "-----\n",
    "\n",
    "Indexes are useful, but lists really shine when you start looping.\n",
    "\n",
    "Loops let you do something for each item in a list. They are kind of like if statements because they have an indented block.\n",
    "\n",
    "They look like this:\n",
    "\n",
    "    for item in list:\n",
    "        print(item) # Do any action per item in the list\n",
    "\n",
    "\"for\" and \"in\" are required. \"list\" can be any variable or literal which is like a list. \"item\" is the name you want to give each item of the list in the indented block as you iterate through. We call each step where item has a new value an iteration.\n",
    "\n",
    "Let's see it in action with our list"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "your_name = \"Albert O'Connor\"\n",
    "awesome_people = [\"Eric Idle\", your_name]\n",
    "awesome_people.append(\"John Cleese\")\n",
    "\n",
    "for person in awesome_people:\n",
    "    print(person)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is bascially the same as writing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "person = awesome_people[0]\n",
    "print(person)\n",
    "person = awesome_people[1]\n",
    "print(person)\n",
    "person = awesome_people[2]\n",
    "print(person)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "But that is a lot more code than:\n",
    "\n",
    "    for person in awesome_people:\n",
    "        print(person)\n",
    "\n",
    "Considering that our list of awesome people could be very long!\n",
    "\n",
    "You can use the built-in function \"range\" to create lists of numbers easily"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "range(0,10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And then we can use that with a loop to print a list of squares."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for number in range(0,10):\n",
    "    print(\"{0} squared is {1}\".format(number, number*number))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Exercise\n",
    "---------\n",
    "\n",
    "Create a list of numbers where every item in the list is the same as its index, i.e. number_list[4] is 4. The list should contain 5 items."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Edit the contents of this list\n",
    "number_list = []\n",
    "\n",
    "# Leave this line alone please\n",
    "check(\"p4\", number_list)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Part 5\n",
    "======\n",
    "\n",
    "Dictionaries\n",
    "------------\n",
    "\n",
    "We have come a long way! Just one more section. Dictionaries are another container like lists, but instead of being index by a number like 0 or 1 it is indexed by a key which can be almost anything. The name comes from being able to use it to represent a dictionary.\n",
    "\n",
    "List literals use square brackets (\"[]\") but dictionaries use braces (\"{}\"). Use \"shift-[\" to type \"{\".\n",
    "\n",
    "    {\"Python\": \"An awesome programming language\", \n",
    "     \"Monty Python\": \"A british comedy troupe\"}\n",
    "\n",
    "In a dictionary the key comes first followed by a colon (\":\") than the value then a comma (\",\") then another key and so on. This is one situation where a colon doesn't start a block.\n",
    "\n",
    "Let's see what running the literal dictionary looks like."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "{\"Python\": \"An awesome programming language\", \n",
    " \"Monty Python\": \"A british comedy troupe\"}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can assign a dictionary to a variable and we can index it by keys to get the values (definitions) out."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "our_dictionary = {\n",
    "   \"Python\": \"An awesome programming language\", \n",
    "   \"Monty Python\": \"A british comedy troupe\"\n",
    "}\n",
    "our_dictionary[\"Python\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can loop over the keys in a dictionary to list all of our definitions..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for key in our_dictionary:\n",
    "    print('The Key is \"{0}\" and the value is  \"{1}\"'.format(key, our_dictionary[key]))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Mail Merge\n",
    "----------\n",
    "\n",
    "Our project this weekend is to write a mail merge program. Given a template for the message and a list of dictionaries of information including email addresses, we can create several messages each personalized depending on the information we have.\n",
    "\n",
    "The data will start as a spreadsheet but it will end up in a dictionary. Each message will have its own dictionary. We can use the string formating you have learned about with a little twist to get Python to format the dictionaries as emails for us.\n",
    "\n",
    "We will look at the basics now, but don't worry will be working on this today and tomorrow!\n",
    "\n",
    "Format with Dictionaries\n",
    "------------------------\n",
    "\n",
    "So far our formatting strings have looked like this:\n",
    "\n",
    "    \"Hello, {0}\"\n",
    "\n",
    "Where \"0\" in \"{0}\" refers to the index of the arguments we pass into the format function. We can also put argument names inbetween the braces \"{}\"."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "print('Hello, {name}! Your favorite color is {favorite_color}.'.format(name=\"Albert O'Connor\", \n",
    "                                                                       favorite_color='green'))\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This is handy if you ever need to reuse a value and you don't want to list it twice.\n",
    "\n",
    "But we can do more, instead of passing keyword arguments into the format function like we did above, we can pass in a dictionary. Note the dictionaries keys have to be strings, but that is exactly what we will be doing."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "info = {'name': \"Albert O'Connor\",\n",
    "        'favorite color': 'green'}\n",
    "print('Hello, {name}! Your favorite color is {favorite color}.'.format(**info))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The \"**\" before data is a bit of stranger Python syntax. It means instead of passing info as the first agrument, pass all the key value pairs inside the info dictionary as keyword arguments. Let's look at a few varation on the syntax one at a time."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = {'name': \"Albert O'Connor\",\n",
    "        'favorite_color': 'green'}\n",
    "print('{0}'.format(data)) # This prints the dictionary as text"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = {'name': \"Albert O'Connor\",\n",
    "        'favorite_color': 'green'}\n",
    "print('{0}'.format(**data)) # This produces an error, there are no indexable arguments, just keyword"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = {'name': \"Albert O'Connor\",\n",
    "        'favorite_color': 'green'}\n",
    "print('{0}'.format('Eric!', **data)) # Eric is the 0th argument, all the keywords are ignored!"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = {'name': \"Albert O'Connor\",\n",
    "        'favorite_color': 'green'}\n",
    "# data is passed as indexiable so there is no keyword arguments.\n",
    "print('Hello, {name}! Your favorite color is {favorite_color}.'.format(data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = {'name': \"Albert O'Connor\",\n",
    "        'favorite_color': 'green'}\n",
    "# There we go, name and favorite_color are passed as keywords.\n",
    "print('Hello, {name}! Your favorite color is {favorite_color}.'.format(**data))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "data = {'name': \"Albert O'Connor\",\n",
    "        'favorite_color': 'green'}\n",
    "# data is passed as keyword, but doesn't have the key 'pet_name'.\n",
    "print('{pet_name}'.format(**data))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The excersises in this section will combine the above idea with lists and loops.\n",
    "\n",
    "Exercises\n",
    "---------\n",
    "\n",
    "First we need some data, in this case we want to create two dictionaries, each with a name and email key:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Edit with the values you want.\n",
    "entry_1 = {'name': \"\",\n",
    "           'email': \"\"}\n",
    "\n",
    "# Add the keys and values to this entry like above.\n",
    "entry_2 = {}\n",
    "\n",
    "print(entry_1)\n",
    "print(entry_2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Once you two dictionaries, entry_1 and entry_2 with values for the keys 'name' and 'email', let's put them into a list."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "entries = [entry_1,]  # Edit this list to include both entries\n",
    "\n",
    "print(entries)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now you can write your own message which uses both the 'name' and 'email' values. You can write anything you want. We are going to use another way to write Python strings, inside triple quote string you can just press enter for a newline, no \"\\n\" required. Just write as you would normally in an editor."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# Edit this message\n",
    "message = \"\"\"To: {email}\n",
    "\n",
    "Hey you,\n",
    "\n",
    "How is the weather?\n",
    "\"\"\"\n",
    "\n",
    "print(message)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Make sure your message includes \"{name}\" somewhere so it is personalized.\n",
    "\n",
    "Final step, let's use a for loop to combine everything! Change the for loop to format the message with each of the entries."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "for entry in entries:\n",
    "    print(message) # Add .format with the right arguments\n",
    "    print(\"-\"*40)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Ask a tutor to review your work and ask any question you have.\n",
    "\n",
    "Once you have your tutors ok you are done! Awesome job!"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python [py35]",
   "language": "python",
   "name": "Python [py35]"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
